﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using TileMapEditor.Tools;

namespace TileMapEditor.Config
{
    public enum TileMapAreaType
    {
        BLOCKED = 0x00,    // blocked area
        WALKING,        // walking area
        OCCLUDED,       // occluded area
        NPC = 0x0F,      // NPC coordinate 

        PATHFIND = 0xFF, // Test Path Finding
    }

    public static class TileMapIds
    {
        public static string blocked = "Blocked";
        public static string walking = "Walking";
        public static string occluded = "Occluded";
        public static string npc = "NPC";
        public static string pathFind = "PathFind";
    }

    [Serializable]
    public class TileMapSettings : ScriptableObject
    {
        public static Color DefaultColor = Color.magenta;

        public static Color GridLineColor = Color.red;
        public static Color OverlayColor = Color.red;

        private static Dictionary<Int32, Color> _area_colors;
        public static Dictionary<Int32, Color> areaColors
        {
            get { return _area_colors; }
        }

        public static Dictionary<string, int> _area_type;
        public static Dictionary<string, int> areaType
        {
            get { return _area_type; }
        }

        public static void LoadPrefs()
        {
            GridLineColor = EditorPrefsWrapper.GetColor( TileMapSettingPrefsKeys.GridLineColor );
            OverlayColor = EditorPrefsWrapper.GetColor( TileMapSettingPrefsKeys.OverlayColor );

            if ( GridLineColor.Equals( DefaultColor ) )
            {
                GridLineColor = Color.red;
                GridLineColor.a = 0.3f;
            }

            if ( OverlayColor.Equals( DefaultColor ) )
            {
                OverlayColor = Color.cyan;
                OverlayColor.a = 0.3f;
            }

            _area_colors = new Dictionary<Int32, Color>() {
                { 0x00, TileMapAreaColors.blocked },
                { 0x01, TileMapAreaColors.walking },
                { 0x02, TileMapAreaColors.occluded },
                { 0x0F, TileMapAreaColors.npc },
                { 0xFF, TileMapAreaColors.pathFind },
            };

            _area_type = new Dictionary<string, int>() {
                { TileMapIds.blocked,   0x00 },
                { TileMapIds.walking,   0x01 },
                { TileMapIds.occluded,  0x02 },
                { TileMapIds.npc,       0x0F },
                { TileMapIds.pathFind,  0xFF },
            };
        }

        private void OnEnable()
        {
            LoadPrefs();
        }

        public static class TileMapSettingPrefsKeys
        {
            public static string GridLineColor = "TileMapEditorSetting-GridLineColor";
            public static string OverlayColor = "TileMapEditorSetting-OverlayColor";
        }

        public static class TileMapAreaColors
        {


            public static Color walking = new Color( 0f, 1f, 0f, 0.58f );
            public static Color occluded = new Color( 1f, 1f, 0f, 0.58f );
            public static Color blocked = new Color( 1f, 1f, 1f, 0.0f );
            public static Color npc = Color.blue;

            public static Color brushBlocked = new Color( 1f, 1f, 1f, 0.4f );

            public static Color pathFind = Color.magenta;
        }

        public static Color GetAreaColor( Int32 val, bool for_brush = true )
        {
            if ( for_brush && val == 0x00 )
            {
                return TileMapAreaColors.brushBlocked;
            }

            if ( areaColors.ContainsKey( val ) )
            {
                return areaColors[val];
            }

            return DefaultColor;
        }

        /// <summary>
        /// Tile map information
        /// </summary>
        [Serializable]
        public class TileSlotType
        {
            /// <summary>
            /// Area Color
            /// </summary>
            public Color color;

            /// <summary>
            /// Area Value
            /// </summary>
            public TileMapAreaType type;

            public TileSlotType( TileMapAreaType t, Color c )
            {
                color = c;
                type = t;
            }
        }
    }
}
